﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
    public class TestShaderProgram {

        /// <summary>
        /// コンストラクターのテスト
        /// </summary>
        /// <remarks>
        /// (*1) まだコンパイル＆リンクされていないのでこの時点ではUniform変数とAttribute変数の数は0.
        /// </remarks>
        [Fact]
        public void Test_New1 () {
            var vs = new VertexShader ("Res/Shaders/simple.vert");
            var fs = new FragmentShader ("Res/Shaders/simple.frag");
            var sp = new ShaderProgram (vs, fs);

            Assert.Equal ("", sp.LinkLog);
            Assert.Equal (0, sp.AttributeCount);       // (*1)
            Assert.Equal (0, sp.UniformCount);         // (*1)
            Assert.Equal (0, sp.FeedbackVaryingCount);
        }

        [Fact]
        public void Test_AddFeedbackVarying () {
            var vs = new VertexShader ("Res/Shaders/simple.vert");
            var fs = new FragmentShader ("Res/Shaders/simple.frag");
            var sp = new ShaderProgram (vs, fs);

            sp.SetTransformFeedbackEnable (true, 2);
            sp.SetFeedbackVarying (0, "Hello");
            sp.SetFeedbackVarying (1, "World");

            Assert.Equal (2, sp.FeedbackVaryingCount);
            Assert.Equal (2, sp.FeedbackVaryings.Count ());
        }

        [Fact]
        public void Test_RemoveFeedbackVarying () {
            var vs = new VertexShader ("Res/Shaders/simple.vert");
            var fs = new FragmentShader ("Res/Shaders/simple.frag");
            var sp = new ShaderProgram (vs, fs);

            sp.SetTransformFeedbackEnable (true, 2);
            sp.SetFeedbackVarying (0, "Hello");
            sp.SetFeedbackVarying (1, "World");
            sp.RemoveFeedbackVarying ("Hello");
            sp.RemoveFeedbackVarying ("World");

            Assert.Equal (2, sp.FeedbackVaryingCount);
            Assert.Equal (0, sp.FeedbackVaryings.Count ());
        }

        [Fact]
        public void Test_References () {
            var vs = new VertexShader ("Res/Shaders/simple.vert");
            var fs = new FragmentShader ("Res/Shaders/simple.frag");
            var sp = new ShaderProgram (vs, fs);

            var objs = sp.References;

            Assert.Equal (2, objs.Count ());
            Assert.Equal (true, objs.All (x => x != null));
        }

        [Fact]
        public void Test_Find () {
            var vs = new VertexShader ("Res/Shaders/simple.vert");
            var fs = new FragmentShader ("Res/Shaders/simple.frag");
            var sp = new ShaderProgram (vs, fs);
            vs.UserID = 101;
            fs.UserID = 102;
            sp.UserID = 103;

            Assert.Equal (vs, sp.Find (101));
            Assert.Equal (fs, sp.Find (102));
            Assert.Equal (sp, sp.Find (103));
        }
    }
}
